/**
* @classDescription 微信公众平台消息推送来来的xml解析工具
* @author laiwp
* @createDate 2016-06-11
* @version 0.1.2
* 微信返回数据格式如下:
<xml><ToUserName><![CDATA[gh_7bf492957076]]></ToUserName>
<FromUserName><![CDATA[olfS6jsyRA83kq0rwEWizr-tliV4]]></FromUserName>
<CreateTime>1366382566</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[369]]></Content>
<MsgId>5868568434794561608</MsgId>
</xml>
*/

var sax = require("sax");
var parser = sax.parser(strict, {
    lowercasetags: true,
    trim: true
}),
strict = true;

var dt = new Array();
var dts = new Array();
var st = '';
//var ToUserName, FromUserName, CreateTime, MsgType, Content, MsgId;
var wxMsg = {};

exports.pars = function(xmlcode, cb) {
    parser.write(xmlcode).close();
    //cb(ToUserName, FromUserName, CreateTime, MsgType, Content, MsgId);
    cb(wxMsg);
};
parser.onerror = function(e) {
    // an error happened.
    console.log('error');
};
parser.ontext = function(t) {
    // if(nm == 'billingtype' || nm == 'introduction')
    // st += '"' + nm +'":"' + t +'"';
    // else st += '"' + nm +'":"' + t +'",';
    st += '"' + nm + '":"' + t + '",';
};
parser.onready = function(a) {
    //console.log('aaaaaaaaaa:' + nm+"----> "+t);	
};

parser.onopentag = function(tag) {
    nm = tag.name;
    //console.log('xml 字段:' + nm);
    if (nm == 'xml') st = '{';
};
parser.onclosetag = function(tag) {
    if (tag == 'xml') {
        st += '}';
        //console.log('文件解析onclosetag完成!!!' + st.substring(st.length-2,st.length-1));
        if (st.substring(st.length - 2, st.length - 1) == ',') {
            var k = st.lastIndexOf(',');
            var str = st.substring(0, k);
            st = str + '}';
        }
        //if(tag == 'mercname')
        console.log('文件解析onclosetag完成0!!!' + st);
        console.log('文件解析onend完成!!!st.length=' + st.length);
        dt.push(JSON.parse(st));
        st = '';
    }
};
parser.oncdata = function(data) {
	//sys.print("<![CDATA["+data+"]]>");
	st += '"' + nm + '":"' + data + '",';
};
parser.oncomment = function(comment) {
    console.log('文件解析oncomment完成!!!');
    //sys.print("<!--"+comment+"-->");
};
parser.onend = function() {
    console.log('文件解析onend完成!!!dt.length=' + dt.length);
    
    for (var i = 0; i < dt.length; i++) {
        //console.log(dt[i]);
        // ToUserName = dt[i].tousername;
        // FromUserName = dt[i].fromusername;
        // CreateTime = dt[i].createtime;
        // MsgType = dt[i].msgtype;
        // Content = dt[i].content;
        // MsgId = dt[i].msgid;
        if(dt[i].msgtype == 'text'){//回来的是文本消息
        	wxMsg = {
	        	"toUserName":dt[i].tousername,
	        	"fromUserName":dt[i].fromusername,
	        	"createTime":dt[i].createtime,
	        	"msgType":dt[i].msgtype,
	        	"content":dt[i].content,
	        	"msgId":dt[i].msgid,
        	};	
        }else if(dt[i].msgtype == 'image'){
        	wxMsg = {
	        	"toUserName":dt[i].tousername,
	        	"fromUserName":dt[i].fromusername,
	        	"createTime":dt[i].createtime,
	        	"msgType":dt[i].msgtype,
	        	"content":dt[i].PicUrl,
	        	"msgId":dt[i].msgid,
        	};		
        }else if(dt[i].msgtype == 'location'){
        	wxMsg = {
	        	"toUserName":dt[i].tousername,
	        	"fromUserName":dt[i].fromusername,
	        	"createTime":dt[i].createtime,
	        	"msgType":dt[i].msgtype,
	        	"location_x":dt[i].location_x,
	        	"location_y":dt[i].location_y,
	        	"content":dt[i].label,
	        	"msgId":dt[i].msgid,
        	};		
        }
        else{
            if(dt[i].event.toLocaleLowerCase() == 'location' ){
                wxMsg = {
                    "toUserName":dt[i].tousername,
                    "fromUserName":dt[i].fromusername,
                    "createTime":dt[i].createtime,
                    "msgType":dt[i].msgtype,
                    "latitude":dt[i].latitude,
                    "longitude":dt[i].longitude,
                    "precision":dt[i].precision,
                    "content":dt[i].event,
                    "msgId":dt[i].msgid,
                };      
            }else{
                wxMsg = {//回来的是事件消息
                    "toUserName":dt[i].tousername,
                    "fromUserName":dt[i].fromusername,
                    "createTime":dt[i].createtime,
                    "msgType":dt[i].msgtype,
                    "content":dt[i].event,
                    "msgId":dt[i].msgid,
                    "eventkey":dt[i].eventkey,
                };    
            }
        	
        }
        

    };

};